package org.gjy.m8.concurrent;

import java.util.concurrent.*;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-06-30 14:43:47
 */
public class ThreadPoolUtil {

    private static final Integer PROCESSORS = Runtime.getRuntime().availableProcessors();

    public static ThreadPoolExecutor getOrderPool() {
        return new ThreadPoolExecutor(
                2,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new DbPolicy()
        );
    }

    public static ThreadPoolExecutor getCustomPool(Integer core) {
        return new ThreadPoolExecutor(
                core,
                core,
                5,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );
    }

    public static ThreadPoolExecutor getDefaultPool() {
        return new ThreadPoolExecutor(
                PROCESSORS,
                PROCESSORS * 2,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(PROCESSORS * 5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );
    }

    public static void shutdown(ThreadPoolExecutor executor) {
        executor.shutdown();
    }

}
